using System;
using Gj.Galaxy.Logic;
using Gj.Services;
using SimpleJSON;
using UnityEngine;

namespace Gj
{
    public enum TaskKey : byte
    {
        Ids,
        DataIndex
    }

    public enum TaskProcessKey : byte
    {
        Id,
        Number
    }

    public class TaskPlugin : GamePlugin
    {
        private BasePage Page;

        public void OnInit(BaseGameSpace space)
        {
            Page = space.UiSpace.Page;
        }

        public void OnDestroy()
        {
        }

        public GameService[] GetRequireService()
        {
            return new GameService[] { EventService.single, DataService.single };
        }

        public void OnUpdate()
        {
        }

        public void LateUpdate()
        {
        }

        public void OnProcessRoom(object[] data)
        {
            // 处理进程
            foreach (object[] detail in data)
            {
                var key = (InfoKey)detail[(byte)DetailKey.Key];
                switch (key)
                {
                    case InfoKey.TaskKey:
                        var taskInfo = (object[])detail[(byte)DetailKey.Value];
                        if (taskInfo == null) break;
                        var ids = new JSONArray();
                        foreach (var id in (object[])taskInfo[(byte)TaskKey.Ids])
                        {
                            Debug.LogFormat("task id:{0}", Convert.ToInt32(id));
                            ids.Add(Convert.ToInt32(id));
                        }

                        Page.Send(SystemMessage.ShowTask, ids);

                        var indexs = (object[])taskInfo[(byte)TaskKey.DataIndex];
                        for (var i = 0; i < indexs.Length; i++)
                            //                            AttrService.single.AddRealtimeIndex( (ushort) Convert.ToInt32(indexs[i]));
                            AttrService.single.AddProcessData((ushort)Convert.ToInt32(indexs[i]));

                        break;
                    case InfoKey.TaskProcessKey:
                        var obj = detail[(byte)DetailKey.Value];
                        if (obj == null) break;
                        var objects = (object[])obj;
                        var o = new JSONObject
                        {
                            { "id", Convert.ToInt32(objects[(byte)TaskProcessKey.Id]) },
                            { "total", Convert.ToInt32(objects[(byte)TaskProcessKey.Number]) }
                        };
                        Debug.LogFormat("task process:id:{0}, process:{1}", o["id"], o["total"]);

                        Page.Send(SystemMessage.UpdateTask, o);
                        break;
                }
            }
        }

        public void OnFinish(object result)
        {
        }

        public void OnSwitchArea()
        {
        }

        public void OnInstance(int targetIndex, byte relation, GamePlayer player, Vector3 position, bool isLocal,
            NetworkEsse master, Action<InitSyncListener> cb)
        {
        }

        public void OnDestroy(NetworkEsse instance, GamePlayer player)
        {
        }

        public void OnBroadcast(GamePlayer player, byte code, Vector3 position, StreamReader reader)
        {
        }

        public void OnEnterRoom(bool first)
        {
        }

        public void OnPlayerJoin(GamePlayer player)
        {
        }

        public void OnPlayerLeave(GamePlayer player)
        {
        }

        public void OnPlayerRejoin(GamePlayer player)
        {
        }

        public void OnFailConnect()
        {
        }

        public void OnReady()
        {
        }

        public void OnReadyPlayer(GamePlayer player)
        {
        }

        public void OnStart()
        {
        }

        public void OnReadyAll()
        {
        }

        public void OnInitGame()
        {
        }

        public void OnTimeMove(int startTime, int endTime, StreamReader reader, Vector3[] positions, int index,
            Vector3 middlePosition)
        {
        }

        public void OnTimeProcess(int startTime, int endTime, StreamReader reader)
        {
        }
    }
}